@ECHO OFF

SETLOCAL ENABLEDELAYEDEXPANSION

SET MainFiles="*.c" "*.cpp" "*.h" "*.rc" "*.vcproj" "*.sln"
SET ProjectFiles="*.sln"

:: Detect project name

SET Project=

DIR /A-D %ProjectFiles% >NUL 2>&1 && (
	FOR /F "tokens=*" %%A IN ('DIR /B /A-D %ProjectFiles%') DO SET Project=%%~nA
)

IF "%Project%"=="" SET /P Project=Project name: 
IF "%Project%"=="" ECHO Unable to determine project name.& EXIT /B 1

:: Found archiver

SET Archiver=
rar >NUL 2>&1 && SET Archiver=rar
IF "%Archiver%"=="" 7z >NUL 2>&1 && SET Archiver=7z
IF "%Archiver%"=="" ECHO No supported archivers ^(rar^, 7z^) found.& EXIT /B 1

:: Calculate latest modification time

CALL :set_DateTimeVars
CALL :get_LastModificationTime "%MainFiles%"

:: Create archive

IF "%Archiver%"=="rar" (
	%Archiver% a -k -rr3 -m5 -s -t -r0 "..\%Project% %LastModificationTime%.rar" "*"
)
IF "%Archiver%"=="7z" (
	%Archiver% a -r -mx9 -ms=on -y -ssw "..\%Project% %LastModificationTime%.7z" "*"
)

SETLOCAL DISABLEDELAYEDEXPANSION

EXIT /B 0


:: ============================================================================================= ::

:get_LastModificationTime

SET LastModificationTime=0000.01.01 00:00
DIR /S /A-D /-C %~1 >NUL 2>&1 && (

	FOR /F "tokens=1-4*" %%A IN ('DIR /S /A-D /-C %~1 ^| FINDSTR /R "[0-9][0-9]:[0-9][0-9]"') DO (

		CALL :get_YearMonthDay "%%~A"
		CALL :get_HoursMinutesSeconds "%%~B %%~C"

		SET NewModificationTime=!Year_txt!.!Month_txt!.!Day_txt! !Hours_txt!-!Minutes_txt!

		IF "!LastModificationTime!" LSS "!NewModificationTime!" (
			SET LastModificationTime=!NewModificationTime!
		)

	)
)

EXIT /B 0


:: ============================================================================================= ::

:set_DateTimeVars

SET RegPath=HKCU\Control Panel\International

FOR /F "tokens=3" %%A IN ('REG QUERY "%RegPath%" /v iDate') DO SET iDate=%%A
FOR /F "tokens=3" %%A IN ('REG QUERY "%RegPath%" /v sDate') DO SET sDate=%%A
FOR /F "tokens=3" %%A IN ('REG QUERY "%RegPath%" /v iTime') DO SET iTime=%%A
FOR /F "tokens=3" %%A IN ('REG QUERY "%RegPath%" /v sTime') DO SET sTime=%%A
FOR /F "tokens=3" %%A IN ('REG QUERY "%RegPath%" /v s2359') DO SET s2359=%%A

EXIT /B 0


:: ============================================================================================= ::

:get_YearMonthDay

SET RawDate=%~1
IF "%RawDate%"=="" SET RawDate=%DATE%

REM ECHO "%RawDate%" | FIND "%sDate%" >NUL 2>&1 || EXIT /B 1

FOR /F "tokens=1-2* delims=%sDate%" %%A IN ("%RawDate%") DO (
	FOR /F "tokens=1*" %%X IN ("%%~A") DO IF NOT "%%~Y"=="" (SET D1=%%~Y) ELSE (SET D1=%%~X)
	SET D2=%%~B
	FOR /F "tokens=1*" %%X IN ("%%~C") DO IF NOT "%%~X"=="" (SET D3=%%~X) ELSE (SET D3=%%~Y)
)

IF "%iDate%"=="0" (
	SET Year=%D3:~0,4%
	SET Month=%D1:~0,2%
    SET Day=%D2:~0,2%
)
IF "%iDate%"=="1" (
    SET Year=%D3:~0,4%
    SET Month=%D2:~0,2%
    SET Day=%D1:~0,2%
)
IF "%iDate%"=="2" (
    SET Year=%D1:~0,4%
    SET Month=%D2:~0,2%
    SET Day=%D3:~0,2%
)

CALL :get_NoLeadingZeroes "%Year%"
SET /A Year = NoLeadingZeroes
SET /A Year_txt = Year + 10000
SET Year_txt=%Year_txt:~1,4%
CALL :get_NoLeadingZeroes "%Month%"
SET /A Month = NoLeadingZeroes
SET /A Month_txt = Month + 100
SET Month_txt=%Month_txt:~1,2%
CALL :get_NoLeadingZeroes "%Day%"
SET /A Day = NoLeadingZeroes
SET /A Day_txt = Day + 100
SET Day_txt=%Day_txt:~1,2%

EXIT /B 0


:: ============================================================================================= ::

:get_HoursMinutesSeconds

SET RawTime=%~1
IF "%RawTime%"=="" SET RawTime=%TIME%

FOR /F "tokens=1-2* delims=%sTime%" %%A IN ("%RawTime%") DO (
	FOR /F "tokens=1*" %%X IN ("%%~A") DO IF NOT "%%~Y"=="" (SET T1=%%~Y) ELSE (SET T1=%%~X)
	FOR /F "tokens=1*" %%X IN ("%%~B") DO IF NOT "%%~X"=="" (SET T2=%%~X) ELSE (SET T2=%%~Y)
	FOR /F "tokens=1*" %%X IN ("%%~C") DO IF NOT "%%~X"=="" (SET T3=%%~X) ELSE (SET T3=%%~Y)
)

IF "%iTime%"=="0" ECHO "%RawTime%" | FIND /I "%s2359%" >NUL 2>&1 && SET /A T1 += 12

SET Hours=%T1%
SET Minutes=%T2%
IF "%T3%"=="" (SET Seconds=0& SET Milisec=0) ELSE (SET Seconds=%T3:~0,2%& SET Milisec=%T3:~3,2%)

CALL :get_NoLeadingZeroes "%Hours%"
SET /A Hours = NoLeadingZeroes
SET /A Hours_txt = Hours + 100
SET Hours_txt=%Hours_txt:~1,2%
CALL :get_NoLeadingZeroes "%Minutes%"
SET /A Minutes = NoLeadingZeroes
SET /A Minutes_txt = Minutes + 100
SET Minutes_txt=%Minutes_txt:~1,2%
CALL :get_NoLeadingZeroes "%Seconds%"
SET /A Seconds = NoLeadingZeroes
SET /A Seconds_txt = Seconds + 100
SET Seconds_txt=%Seconds_txt:~1,2%
CALL :get_NoLeadingZeroes "%Milisec%"
SET /A Milisec = NoLeadingZeroes
SET /A Milisec_txt = Milisec + 100
SET Milisec_txt=%Milisec_txt:~1,2%


EXIT /B 0


:: ============================================================================================= ::

:get_NoLeadingZeroes

SET NoLeadingZeroes=%~1

:remove_FirstZero
IF /I "%NoLeadingZeroes:~0,1%"=="0" IF /I NOT "%NoLeadingZeroes:~1,1%"=="" (
	SET NoLeadingZeroes=%NoLeadingZeroes:~1%
	GOTO :remove_FirstZero
)

EXIT /B 0



:: ============================================================================================= ::
